package com.yc.security.authentication;

import java.io.IOException;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.yc.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;


/**
 * 未登录 处理器
 * @author kukaha
 * @version 1.0.0
 * @ClassName JwtAuthenticationEntryPoint.java
 * @Description TODO
 * @createTime 2021年03月27日 00:01:00
 */
@Component("jwtAuthenticationEntryPoint")
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {

	Logger logger = LoggerFactory.getLogger(getClass());

	private static final long serialVersionUID = 1L;

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException {
		logger.info("authentication: " + JSON.toJSONString(authException));
		Result result = Result.error().code(HttpServletResponse.SC_UNAUTHORIZED).message("无访问权限,请先登录!!!");
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().write(result.buildString());
	}
}
